package com.utils
{

	/**
	 * 	Class that contains static utility methods for manipulating and working
	 *	with Arrays.
	 *
	 *	Note that all APIs assume that they are working with well formed arrays.
	 *	i.e. they will only manipulate indexed values.
	 *
	 * 	@langversion ActionScript 3.0
	 *	@playerversion Flash 9.0
	 *	@tiptext
	 */
	public class ArrayUtil
	{

		/**
		 *  查找数组中是否有该对象，没有返回true有返回false
		 * 	@langversion ActionScript 3.0
		 *	@playerversion Flash 9.0
		 *	@tiptext
		 */
		public static function arrayContainsValue(arr : Array, value : Object) : Boolean
		{
			return (arr.indexOf(value) != -1);
		}

		/**
		 *  移除数组中的对象
		 * 	@langversion ActionScript 3.0
		 *	@playerversion Flash 9.0
		 *	@tiptext
		 */
		public static function removeValueFromArray(arr : Array, value : Object) : void
		{
			var len : uint = arr.length;

			for (var i : Number = len; i > -1; i--)
			{
				if (arr[i] === value)
				{
					arr.splice(i, 1);
				}
			}
		}

		/**
		 *  过滤掉重复的对象
		 * 	@langversion ActionScript 3.0
		 *	@playerversion Flash 9.0
		 *	@tiptext
		 */
		public static function createUniqueCopy(a : Array) : Array
		{
			var newArray : Array = new Array();

			var len : Number = a.length;
			var item : Object;

			for (var i : uint = 0; i < len; ++i)
			{
				item = a[i];

				if (arrayContainsValue(newArray, item))
				{
					continue;
				}

				newArray.push(item);
			}

			return newArray;
		}

		/**
		 *  复制一个新数组
		 * 	@langversion ActionScript 3.0
		 *	@playerversion Flash 9.0
		 *	@tiptext
		 */
		public static function copyArray(arr : Array) : Array
		{
			return arr.slice();
		}

		/**
		 *  检查2个数组是否相等
		 * 	@langversion ActionScript 3.0
		 *	@playerversion Flash 9.0
		 *	@tiptext
		 */
		public static function arraysAreEqual(arr1 : Array, arr2 : Array) : Boolean
		{
			if (arr1.length != arr2.length)
			{
				return false;
			}

			var len : Number = arr1.length;

			for (var i : Number = 0; i < len; i++)
			{
				if (arr1[i] !== arr2[i])
				{
					return false;
				}
			}

			return true;
		}
	}
}
